<?php
require_once(ROOT_ADMIN_MODELS.DS.'Models_Base.php'); 
require_once(ROOT_ADMIN_MODELS.DS.'Models_Log.php'); 
require_once(ROOT_ADMIN_MODELS.DS.'Models_Typenews.php');
require_once(ROOT_LIBRARY_UTILITY.DS.'utility.php');
require_once(ROOT_ADMIN_CONTROLLERS.DS.'BaseController.php');
require_once(ROOT_ADMIN_FORMS.DS.'Forms_Typenews.php');
require_once(ROOT_ADMIN_OBJECT.DS.'Objs_Typenews.php');

class Admin_TypenewsController extends BaseController {	
		public function preDispatch()
		{
			parent::preDispatch();
			if(!$this->hasPrivilege())
				$this->_redirect ("/admin/error/permission");
		}
		
	
	public function newAction()
	{
		$this->view ->headTitle("UIT-News Supporter");
		try
		{
			require_once(ROOT_ADMIN_MODELS.DS.'Models_Forum.php');
			$mdforum = new Models_Forum();
			$arrforum = $mdforum->listforum("sort");
			$this->view->arrforum = $arrforum;
			$forum_id = $this->_request->getParam('forum_id');
			$this->view->forum_id = $forum_id;
			if($this->_request->isPost())
			{
				$md = new Models_Typenews();
				$form = new Forms_Typenews();
				$form->_requestToForm($this);
				$form->obj->forum_id = $_POST['forum_id'];
				$id = $md->_insert($form->obj);
				Models_Log::insert($this->view->user->id, "act_new_Typenews","id=".$id);
				$this->_redirect("/admin/forum/edit/id/".$form->obj->forum_id);
			}
		}
		catch (Exception $ex)
		{
			$this->view->errMsg = $ex->getMessage();
			Utility::log($ex->getMessage(), $ex->getFile(), $ex->getLine());
		}
	}
	
	public function editAction()
	{
		$this->view ->headTitle("UIT-News Supporter");
		try{
			require_once(ROOT_ADMIN_MODELS.DS.'Models_Forum.php');
			$mdforum = new Models_Forum();
			$arrforum = $mdforum->listforum("sort");
			$this->view->arrforum = $arrforum;
			$id = $this->_request->getParam('id');
			$md = new Models_Typenews();
			$typenews = $md->gettypenewsByid($id);
			$this->view->typenews = $typenews;
			if($this->_request->isPost())
			{
				$md = new Models_Typenews();
				$form = new Forms_Typenews();
				$form->_requestToForm($this);
				$md->_update($form->obj);
				$typenews = $md->gettypenewsByid($id);
				$this->view->typenews = $typenews;
				$this->_redirect("/admin/forum/edit/id/".$form->obj->forum_id);
				Models_Log::insert($this->view->user->id, "act_edit_Typenews","id=".$id);
			}
		}
		catch(Exception $ex){
			$this->view->errMsg = $ex->getMessage();
			Utility::log($ex->getMessage(), $ex->getFile(), $ex->getLine());
		}
	}
	
	public function statusAction()
	{
		try{
			$this->_helper->layout->disableLayout();
			$this->_helper->viewRenderer->setNorender();
			$md = new Models_Typenews();
			$idforum = $this->_request->getParam('idforum');
			$id = $_POST['id'];
			$status = $_POST['status'];
			$md->setstatus($id, $status);
			$arrtypenewssort = $md->listtypenews($idforum, "sort");
			echo (json_encode($arrtypenewssort));
			Models_Log::insert($this->view->user->id, "act_Update_Status_Typenews","id=".$id);
		}
		catch(Exception $ex){
			$this->view->errMsg = $ex->getMessage();
			Utility::log($ex->getMessage(), $ex->getFile(), $ex->getLine());
		}
	}
	public function deleteAction()
	{
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNorender();
		try{
			$md = new Models_Typenews();
			$id = $_POST['id'];
			$count = $md->countnews($id);
			if($count == 0)
				$md->del($id);
			else
				$md->setstatus($id,-1);
			echo "1";
			Models_Log::insert($this->view->user->id, "act_Delete_Typenews","id=".$id);
		}
		catch(Exception $ex){
			$this->view->errMsg = $ex->getMessage();
			Utility::log($ex->getMessage(), $ex->getFile(), $ex->getLine());
		}
	}
	public function sortAction()
	{
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNorender();
		try{
			$md = new Models_Typenews();
			foreach ($_POST['id'] as $key=>$row)
			{
				$md->updatenumber($row, $key+1);
			}
			Models_Log::insert($this->view->user->id, "act_Sort_Typenews");
		}
		catch(Exception $ex){
			$this->view->errMsg = $ex->getMessage();
			Utility::log($ex->getMessage(), $ex->getFile(), $ex->getLine());
		}
	}
}
	